﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for UrlUtils
/// </summary>
public class UrlUtils
{
    public static string EncodeToBase64(string txt)
    {
        byte[] toEncodeAsBytes

            = System.Text.Encoding.UTF8.GetBytes(txt);

        string returnValue

              = System.Convert.ToBase64String(toEncodeAsBytes);

        return returnValue;
    }

    static public string DecodeFrom64(string encodedData)
    {

        try
        {
            byte[] encodedDataAsBytes

               = System.Convert.FromBase64String(encodedData);

            string returnValue =

               System.Text.Encoding.UTF8.GetString(encodedDataAsBytes);

            return returnValue;
        }
        catch (Exception ex)
        {
            return string.Empty;
        }

    }

    public static string EncodeUserID(int id)
    {
        return EncodeToBase64(id.ToString());
    }

    public static int? DecodeUserID(string strID)
    {
        var decoded = DecodeFrom64(strID);
        int id = 0;
        if (int.TryParse(decoded, out id))
            return id;
        else return null;
    }
}